package com.ruoyi.web.controller.tool;

import java.io.*;
import java.net.*;

public class ImageSpider {
    public static void main(String[] args) {
        // 确定要爬取的网站URL和本地保存目录
        String url = "https://blog.csdn.net/yelangjdjjsjeh/article/details/120510295?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-120510295-blog-107393556.235%5Ev43%5Epc_blog_bottom_relevance_base2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-120510295-blog-107393556.235%5Ev43%5Epc_blog_bottom_relevance_base2&utm_relevant_index=1";
        String saveDir = "D:/images/";

        // 读取网页HTML源代码
        String html = getHtml(url);

        // 提取其中的图片URL
        String[] imgUrls = extractImgUrls(html);

        // 下载图片到本地
        for (String imgUrl : imgUrls) {
            downloadImage(imgUrl, saveDir);
        }
    }

    // 读取网页HTML源代码
    public static String getHtml(String url) {
        StringBuilder html = new StringBuilder();
        try {
            URLConnection conn = new URL(url).openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                html.append(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html.toString();
    }

    // 提取图片URL
    public static String[] extractImgUrls(String html) {
        String[] imgUrls = html.split("<img");
        String[] result = new String[imgUrls.length - 1];
        for (int i = 1; i < imgUrls.length; i++) {
            String imgUrl = imgUrls[i].substring(imgUrls[i].indexOf("src=") + 5);
            imgUrl = imgUrl.substring(0, imgUrl.indexOf("\""));
            result[i - 1] = imgUrl;
        }
        return result;
    }

    // 下载图片到本地
    public static void downloadImage(String imgUrl, String saveDir) {
        try {
            URL url = new URL(imgUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();
            byte[] data = readInputStream(inStream);
            File file = new File(saveDir + imgUrl.substring(imgUrl.lastIndexOf("/") + 1));
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(data);
            outStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取输入流并转换为字节数组
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        inputStream.close();
        return outputStream.toByteArray();
    }
}
